/**
 * create by young (young2f@outlook.com) on 2020/2/26
 */
'use strict';
import server from '../services/treeNode';

export default {
  namespace: 'treeNode',
  state: {
    nodes: [],
    selectId: '',
    selectedNodeInfo: {},
  },
  reducers: {
    onDataLoad(state, { payload }) {
      return { ...state, ...payload };
    },
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },

  effects: {
    * getTree({ payload }, { call, put }) {
      const response = yield call(server.getTree, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: { nodes: response.result },
        });
      }
      return response;
    },

    * getTrees({ payload }, { call, put }) {

      const response = yield call(server.getTrees, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: { nodes: response.result },
        });
      }
      return response;
    },
    /**
     * 获取所有节点，子节点不挂在父节点上
     * @param payload
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */* getNodes({ payload }, { call, put }) {
      const response = yield call(server.getNodes, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: { nodes: response.result },
        });
      }
      return response;
    },
    /**
     * 获取目录信息，并进行租户校验
     * 跨租户访问
     * @param payload:tenantCode,relatingTenantCode
     * @param call
     * @param put
     * @returns {IterableIterator<*>}
     */
    * findTreesWithAuth({ payload }, { call, put }) {
      const response = yield call(server.findTreesWithAuth, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: { nodes: response.result },
        });
      }
      return response;
    },
    * getData({ payload }, { call, put }) {
      const { resultKey } = payload;
      let _key = resultKey ? resultKey : 'treeData';
      delete payload['resultKey'];
      const response = yield call(server.getData, payload);
      const _payload = {};
      _payload[_key] = response.result;
      if (response) {
        yield put({
          type: 'save',
          payload: _payload,
        });
      }
      return response;
    },

    * updateNode({ payload }, { call, put }) {
      const nodes = yield call(server.updateNode, payload);
      return nodes;
    },

    * insert({ payload }, { call, put }) {
      const nodes = yield call(server.insert, payload);
      return nodes;
    },
    * insertBatch({ payload }, { call, put }) {
      const response = yield call(server.insertBatch, payload);
      return response;
    },
    * clear({ payload }, { call, put }) {
      const response = yield call(server.clear, payload);
      return response;
    },
    * deleteNode({ payload }, { call }) {
      const nodes = yield call(server.deleteNode, payload);
      return nodes;
    },
    * moveNode({ payload }, { call, put }) {
      const res = yield call(server.moveNode, payload);
      return res;
    },
    * copyNode({ payload }, { call, put }) {
      const res = yield call(server.copyNode, payload);
      return res;
    },
    * moveTreeData({ payload }, { call }) {
      const res = yield call(server.moveTreeData, payload);
      return res;
    },
    * setConfig({ payload }, { call }) {
      const res = yield call(server.setConfig, payload);
      return res;
    },



    /////////////////

    * getAllPlus({ payload }, { call }) {
      const response = yield call(server.getAll, payload);

      if (response) {
        return response.result;
      }
    },
    * getNodeById({ payload }, { call }) {
      const response = yield call(server.getNodeById, payload);
      if (response) {
        return response.result;
      }
    },


    * getInfoById({ payload }, { call, put }) {
      const res = yield call(server.getInfoById, payload);
      yield put({
        type: 'save',
        payload: { selectedNodeInfo: res.result },
      });
    },


    * getMaterialTreeData({ payload }, { call, put }) {
      const { resultKey } = payload;
      let _key = resultKey ? resultKey : 'materials';
      delete payload['resultKey'];
      const response = yield call(server.getMaterialTreeData, payload);
      const _payload = {};
      _payload[_key] = response.result;
      if (response) {
        yield put({
          type: 'save',
          payload: _payload,
        });
      }
      return response;
    },
  },
};
